package com.guoyu.project.web.admin;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.utils.JsonUtil;
import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.Project_Dynamic;
import com.guoyu.project.model.Project_Manage;
import com.guoyu.project.service.FileUploadService;
import com.guoyu.project.service.Project_DynamicService;

@Controller
@RequestMapping("admin/PDynamic")
public class Project_DynamicController {
	
	    //日志
	    private final static Logger logger = LoggerFactory.getLogger(Project_DynamicController.class);

	    @Resource
	    private Project_DynamicService project_DynamicService;
	    
	    @Resource
	    private FileUploadService fileUploadService;

	    
	    @RequestMapping("index")
	    public String index() {
	        return "admin/PDynamic/index";
	    }


	    /**
	     * pm信息列表页
	     *
	     * @return
	     */
	    @RequestMapping("pdlist")
	    @ResponseBody
	    public Pagination<Project_Dynamic> pmlist(@RequestParam(defaultValue = "0") int offset,
	            @RequestParam(defaultValue = "10") int limit, Model model) {
	        Map<String, Object> params = Maps.newHashMap();
	        return project_DynamicService.queryAllProject_Dynamic(offset, limit, params);
	    }

	    /**
	     * 跳到修改信息页面
	     *
	     * @return
	     */
	    @RequestMapping("edit")
	    public String edit(Integer pd_id, Model model) {
	        Project_Dynamic projectDynamic = project_DynamicService.queryById(pd_id);
	        model.addAttribute("projectDynamic", projectDynamic);
	        System.out.println(projectDynamic);
	        return "admin/PDynamic/edit";
	    }

	    /**
	     * 更新
	     *
	     * @return
	     */
	    @RequestMapping("update")
	    @ResponseBody
	    public APIResponse update(@RequestParam(value = "DynamicPic", required = false) MultipartFile picture,
	    		@RequestParam(value = "DynamicUpdateFil111", required = false)MultipartFile fiFile, 
	    		@RequestParam(value = "DynamicUpdateFil211", required = false)MultipartFile seFile, 
	    		@RequestParam(value = "DynamicUpdateFil311", required = false)MultipartFile thFile, 
	    		Project_Dynamic project_Dynamic) {
	        logger.info("提交修改project_manage信息信息：{}", JsonUtil.encode(project_Dynamic));
	        String picPath = StringUtils.defaultIfBlank(fileUploadService.upload(picture), project_Dynamic.getPicture());
	        project_Dynamic.setPicture(picPath);
	      //文件上传
	        String fifilePath=fileUploadService.upload(fiFile);
	        project_Dynamic.setFiFile(fifilePath);
	        
	        String sefilePath=fileUploadService.upload(seFile);
	        project_Dynamic.setSeFile(sefilePath);
	        
	        String thfilePath=fileUploadService.upload(thFile);
	        project_Dynamic.setThFile(thfilePath);
	        
	        project_DynamicService.updateProject_Dyanmic(project_Dynamic);
	        return APIResponse.returnSuccess();
	    }

	    /**
	     * 删除
	     *
	     * @return
	     */
	    @RequestMapping("delete")
	    @ResponseBody
	    public APIResponse delete(Integer pd_id) {
	    	project_DynamicService.deleteById(pd_id);
	        return APIResponse.returnSuccess();
	    } 
	    
	    
	    @RequestMapping("dynamic")
	    @ResponseBody
	    public Pagination<Project_Dynamic> dynamic(Integer id,Model model,@RequestParam(defaultValue = "0") int offset,
	            @RequestParam(defaultValue = "10") int limit) {
	    	Project_Dynamic dynamic=new Project_Dynamic();
	    	dynamic.setPmanage_id(id);
	    	Map<String, Object> params = Maps.newHashMap();
	    	params.put("dynamic", dynamic);
	    	params.put("pmanage_id", dynamic.getPmanage_id());
	    	return project_DynamicService.queryBypmanageId(offset, limit, params);
	    }
	    
	    
	    @RequestMapping("dymic")
	    public String todymic(Integer id,Model model) {
	    	model.addAttribute("id", id);
	        return "admin/PDynamic/dymic";
	    }


	}